function addUser() {
    $('#add-user-form').submit();
}

$(document).ready(function () {

        $('#add-user-form').submit(function ()//提交表单
        {
            var options = {
                url: '/admin/addUser', //提交给哪个执行
                type: 'POST',
                success: function (data) {
                    alert("添加成功！");
                    $("#user-list").prepend(data);
                    $("#add-user-dialog").modal("hide");
                } //显示操作提示
            };
            $('#add-user-form').ajaxSubmit(options);
            return false; //为了不刷新页面,返回false，反正都已经在后台执行完了，没事！

        });

        $('#update-user-info-form').submit(function ()//提交表单
        {
            var options = {
                url: '/admin/updateUserInfo',
                type: 'POST',
                success: function (data) {
                    alert("修改成功！");
                    //$("#user-list").prepend(data);

                    var newInfo = data.data;

                    $("#user-data-" + toUpdateUserId).attr("data", "(" + JSON.stringify(newInfo) + ")");

                    var oldInfoDivs = $("#user-" + toUpdateUserId + " > div");

                    oldInfoDivs.eq(2).html(newInfo.name);

                    oldInfoDivs.eq(3).html(newInfo.gender);

                    oldInfoDivs.eq(4).html(newInfo.maritalStatus);

                    oldInfoDivs.eq(6).html(newInfo.phoneNum);



                    //$("#update-user-type-"+type).attr("checked",true);
                    $("#update-user-info-dialog").modal("hide");
                } //显示操作提示
            };
            $('#update-user-info-form').ajaxSubmit(options);
            return false; //为了不刷新页面,返回false，反正都已经在后台执行完了，没事！

        });
        $("#user-list").css("height", getClientHeight() - 212).css("maxHeight", getClientHeight() - 120);

        $(window).resize(function () {
            $("#user-list").css("height", getClientHeight() - 212).css("maxHeight", getClientHeight() - 120);
            $("#rightContent").css("width", $(window).width() - 120);
        });

        $("#user-page-select").change(function () {

            var page = $(this).children('option:selected').val() - 1;

            userListTurnPage(page);
        });

        getUserCount();
        userListTurnPage(0);

        $('input[id=import-user]').change(function () {
            $('#file-name').val($(this).val());
        });

        $("#import-user-form").submit(function () {
            var options = {
                url: '/admin/importUsers', //提交给哪个执行
                type: 'POST',
                success: function (data) {



                    //alert("已导入"+data.data+"个用户！");
                    $("#user-list").prepend(data);
                    //$(window).reload(true);


                }

            };

            $("#import-user-form").ajaxSubmit(options);

            return false;

        });

    }
);

var toDeleteUserId;

function beforeDeleteUser(userId) {
    toDeleteUserId = userId;
}

function deleteUser() {
    $.get("/admin/deleteUser?" + "userId=" + toDeleteUserId, function (data) {


        //alert(data.code);

        if (data.code === 1) {
            alert("删除成功！");
            $("#user-" + toDeleteUserId).remove();
        }
        else {
            alert("删除失败！");

        }

        $("#deleteUserAlert").modal('hide');
        // $("#user-list").empty();
        // $("#user-list").append(data);
    });
}

function userPreviousPage() {
    userListTurnPage(currentPage - 1);
    $("#user-page-select").val(currentPage)
}

function userNextPage() {
    userListTurnPage(currentPage + 1);
    $("#user-page-select").val(currentPage + 2);
}

function userFirstPage() {
    userListTurnPage(0);
    $("#user-page-select").val(1);
}

function userLastPage() {
    userListTurnPage(totalPage - 1);
    $("#user-page-select").val(totalPage);
}


var totalPage;


function getUserCount() {
    $.get("/admin/getUserCount", function (data) {
        var pageSelect = $("#user-page-select");
        pageSelect.empty();
        totalPage = parseInt((data.data - 1) / 20 + 1);
        $("#user-total-page").html(totalPage);
        for (var i = 0; i < totalPage; i++) {
            pageSelect.append("<option>" + (i + 1) + "</option>");
        }
        //currentPage = page;
    });
}

var currentPage = 0;

function userListTurnPage(page) {
    $.get("/admin/getUserList?" + "offset=" + page * 20 + "&count=20", function (data) {
        $("#user-list").empty();
        $("#user-list").append(data);

        currentPage = page;

        if (currentPage == 0) {
            $("#user-previous-page").attr("disabled", "disabled");
            $("#user-first-page").attr("disabled", "disabled");
        }
        else {
            $("#user-previous-page").removeAttr("disabled");
            $("#user-first-page").removeAttr("disabled");
        }

        if (currentPage >= totalPage - 1) {
            $("#user-next-page").attr("disabled", "disabled");
            $("#user-last-page").attr("disabled", "disabled");
        }
        else {
            $("#user-next-page").removeAttr("disabled");
            $("#user-last-page").removeAttr("disabled");
        }

    });
}

var toUpdateUserId;

function beforeUpdateUserInfo(userId) {
    toUpdateUserId = userId;

    var info = eval($("#user-data-" + userId).attr("data"));

    $("#update-user-email").val(info.email);
    $("#update-user-id").val(userId);
    $("#update-user-name").val(info.name);
    $("#update-user-type-" + info.type).attr("checked", true);
    $("#update-user-gender-" + (info.gender == "男" ? 0 : 1)).attr("checked", true);
    $("#update-user-marital-status").val(info.maritalStatus);
    $("#update-user-phone-num").val(info.phoneNum);
    $("#update-user-birthday").val(info.birthday);

}

function updateUserInfo() {
    $('#update-user-info-form').submit();
}